package GUI;

import javax.swing.JOptionPane;
import napakalaki.*;

/**
 *
 * @author jonnny0
 */
public class NapakalakiView extends javax.swing.JFrame {

    /**
     * Creates new form NapakalakiView
     */
    private Napakalaki napakalakiModel = new Napakalaki();
    //Variable para saber el monstruo del combate anterior
    Monster monstruoAnterior = null;
    private boolean combatido;

    public NapakalakiView() {
        initComponents();
    }

    public void setNapakalaki(Napakalaki valor) {

        napakalakiModel = valor;
        playerView1.setNapakalakiModel(napakalakiModel);
        playerView1.setPlayer(napakalakiModel.getCurrentPlayer());
        monsterView1.setMonster(napakalakiModel.getCurrentMonster());
        combatido = false;
        repaint();
        revalidate();                
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nextTurn = new javax.swing.JButton();
        combat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        monsterView1 = new GUI.MonsterView();
        combatResult = new javax.swing.JLabel();
        playerView1 = new GUI.PlayerView();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        nextTurn.setText("Next Turn");
        nextTurn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextTurnActionPerformed(evt);
            }
        });

        combat.setText("Combat");
        combat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combatActionPerformed(evt);
            }
        });

        jLabel1.setText("COMBAT RESULT:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(combat)
                                .addGap(18, 18, 18)
                                .addComponent(nextTurn))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(combatResult, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))))
                        .addGap(34, 34, 34)
                        .addComponent(monsterView1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(playerView1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(295, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(playerView1, javax.swing.GroupLayout.PREFERRED_SIZE, 605, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(combat)
                            .addComponent(nextTurn))
                        .addGap(22, 22, 22)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(combatResult, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(monsterView1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void combatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combatActionPerformed
        // TODO add your handling code here:

        if (monstruoAnterior != napakalakiModel.getCurrentMonster()) {
            monstruoAnterior = napakalakiModel.getCurrentMonster();
            CombatResult resultado = napakalakiModel.developCombat();
            //playerView1.repaint();
            combatResult.setText(resultado.toString());
            if (napakalakiModel.endOfGame(resultado)) {
                JOptionPane.showMessageDialog(null, "¡¡¡HAS GANADO EL JUEGO!!!!");
            } else if (resultado == CombatResult.LoseAndDie) {
                JOptionPane.showMessageDialog(null, "¡¡¡ESTAS MUERTO!!!!");
            } else if (resultado == CombatResult.LoseAndConvert) {
                JOptionPane.showMessageDialog(null, "¡¡¡HAS PERDIDO Y TE CONVIERTES EN SECTARIO!!!!");
            }
        } else {
            JOptionPane.showMessageDialog(null, "¡¡¡YA HAS LUCHADO CONTRA ESTE MONSTRUO, DEBES DE PASAR DE TURNO!!!!");
        }
        setNapakalaki(napakalakiModel);
        combatido = true;
        repaint();
    }//GEN-LAST:event_combatActionPerformed

    private void nextTurnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTurnActionPerformed
        // TODO add your handling code here:

        if (!combatido) {
            JOptionPane.showMessageDialog(null, "¡¡NO SEAS COBARDE, HAY QUE COMBATIR ANTES DE PASAR TURNO!!!");
        } else {
            napakalakiModel.getCurrentPlayer().getPendingBadStuff();   //borra ****************************
            if (napakalakiModel.nextTurn()) {
                setNapakalaki(napakalakiModel);
            } else {
                //luego BORRAR ESTO++++++++++++++++++++++++++++++++++++++++++++
                napakalakiModel.getCurrentPlayer().getPendingBadStuff();
                //luego BORRAR ESTO++++++++++++++++++++++++++++++++++++++++++++
                JOptionPane.showMessageDialog(null, "¡¡NO PUEDES PASAR DE TURNO, TE TIENES QUE DESCARTARTE DE OCULTAS!!!");
            }
            //playerView1.repaint();
            //monsterView1.repaint();
            //setNapakalaki(napakalakiModel);
            combatResult.setText(" ");
            repaint();
        }
    }//GEN-LAST:event_nextTurnActionPerformed

    /**
     * @param args the command line arguments
     */
    public void showView() {

        this.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton combat;
    private javax.swing.JLabel combatResult;
    private javax.swing.JLabel jLabel1;
    private GUI.MonsterView monsterView1;
    private javax.swing.JButton nextTurn;
    private GUI.PlayerView playerView1;
    // End of variables declaration//GEN-END:variables
}
